package com.wu.basic.dynamic.controller;

import com.alibaba.fastjson.JSON;
import com.wu.basic.dynamic.domain.uo.ChineseCharacters;
import com.wu.basic.dynamic.util.TTSUtil;
import com.wu.framework.inner.layer.web.EasyController;
import com.wu.framework.inner.lazy.database.expand.database.persistence.LazyOperation;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author : 吴佳伟
 * @version 1.0
 * @describe : 新华字典Controller
 * @date : 2021/2/28 1:27 下午
 */
@EasyController("/xinhua")
public class XinHuaDictionaryController implements CommandLineRunner {

    private final LazyOperation lazyOperation;
    private final RestTemplate restTemplate = new RestTemplateBuilder().build();

    public XinHuaDictionaryController(LazyOperation lazyOperation) {
        this.lazyOperation = lazyOperation;
    }


    /**
     * description 解析新华字典文字
     *
     * @param
     * @return
     * @exception/throws
     * @author 吴佳伟
     * @date 2021/5/7 4:20 下午
     */
    @PostMapping()
    public void saveWord() {
        String jsonStr = "";
        try {
            File file = ResourceUtils.getFile("classpath:static/data/word.json");
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            jsonStr = jsonStr.replace("'", "’");
            final List<ChineseCharacters> charactersList = JSON.parseArray(jsonStr, ChineseCharacters.class);
            lazyOperation.upsert(charactersList);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }


    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {

        System.out.println("自动翻译中文语音开始");
        List<ChineseCharacters> easyHashMapList = lazyOperation.executeSQL("select strokes,update_time,pin_yin,radicals,create_time,more,old_word,id,explanation,word from chinese_characters where voice is  null limit 100 ", ChineseCharacters.class);
        for (ChineseCharacters characters : easyHashMapList) {
            String word = characters.getWord();
            final File file = TTSUtil.baiduTextToSpeech(word);
            characters.setVoice(file);
        }
        if (!ObjectUtils.isEmpty(easyHashMapList)) {
            lazyOperation.upsert(easyHashMapList);
            run(args);
        }
        System.out.println("自动翻译中文语音结束");

    }

    @PostConstruct
    public void inti() {
        final ChineseCharacters normal = new ChineseCharacters();
        normal.setWord("测试字段");
        lazyOperation.smartUpsert(normal);
    }
}
